<!DOCTYPE html>
<html lang='en'>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <!-- 准备好容器 -->
  <div id="test"></div>

  <!--引入react核心库-->
  <script src="../react.js-16/react.development.js" type="text/javascript"></script>
  <!--引入react-dom，用于支持react操作DOM-->
  <script src="../react.js-16/react-dom.development.js" type="text/javascript"></script>
  <!--引入label，用于将jsx转为js-->
  <script src="../react.js-16/babel.min.js" type="text/javascript"></script>
  <!--引入prop-types，用于对组件标签属性的限制-->
  <script src="../react.js-16/prop-types.js" type="text/javascript"></script>

  <script type="text/babel">
    //生命周期回调函数 <=> 生命周期钩子函数 <=> 生命周期函数 <=> 生命周期钩子
    //1.创建组件>
    class Life extends React.Component {
      state = { opacity: 1 }

      death = () => {
        //卸载组件
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      //组件挂载页面之后调用
      componentDidMount() {
        this.timer = setInterval(() => {
          //获取原状态
          let { opacity } = this.state
          //减小0.1
          opacity -= 0.1
          if (opacity <= 0) opacity = 1
          //设置新的透明度
          //this.setState({opacity:opacity})
          this.setState({ opacity: opacity })
        }, 200)
      }

      //组件将要被卸载的时候
      componentWillUnmount() {
        //清除定时器
        clearInterval(this.timer)
      }

      //render调用的时机：初始化渲染、状态更新之后
      render() {
        return (
          <div>
            <h2 style={{ opacity: this.state.opacity } }>React 学不会怎么办</h2>
            <button onClick={this.death}>不活了</button>
          </div>
        )
      }
    }
    //2.渲染组件到页面
    ReactDOM.render(<Life />, document.getElementById('test'))
  </script>
</body>

</html>